import { defineStore } from 'pinia'
import router from '@/router'

export const useMenuStore = defineStore({
  id: 'menu',

  state: () => ({
    navs: [] as any[],
    siders: [] as any[],
    sidebarShow: true,
    switching: false,
    changing: false,
    activeSider: null as any
  }),

  actions: {
    setNavs(data: any) {
      this.navs = data
    },
    setChanging(val: boolean) {
      this.changing = val
    },
    updateSiders(data: any) {
      // console.log(data)
      // 根据nav获取siders
      this.switching = true
      setTimeout(() => {
        // 模拟一个loading的效果
        this.switching = false
      }, 600)
      // console.log(JSON.parse(JSON.stringify(this.navs)))
      const siders = this.navs.find((item: any) => item.path === data.nav)?.children || []
      // console.log(siders)
      this.siders = siders
      let defaultRoute = ''
      this.activeSider = null
      if (!data.path) {
        if (siders.length) {
          // 选择一个默认的路径进行跳转
          if (siders.length === 1) {
            if (siders[0].children?.length > 0) {
              // 需要侧边栏
              this.sidebarShow = true
              defaultRoute = `/${data.nav}/${siders[0].children[0].path}`
              this.activeSider = siders[0].children[0]
            } else {
              // 不需要侧边栏
              this.sidebarShow = false
              defaultRoute = `/${data.nav}/${siders[0].path}`
              this.activeSider = siders[0]
            }
          } else {
            // 需要侧边栏
            this.sidebarShow = true
            if (siders[0].children?.length > 0) {
              defaultRoute = `/${data.nav}/${siders[0].children[0].path}`
              this.activeSider = siders[0].children[0]
            } else {
              defaultRoute = `/${data.nav}/${siders[0].path}`
              this.activeSider = siders[0]
            }
          }
        } else {
          // 没有可用的跳转路径
          router.push('/not-auth')
        }
      } else {
        defaultRoute = data.path
        if (siders.length) {
          if (siders.length === 1) {
            if (siders[0].children?.length > 0) {
              this.sidebarShow = true
              this.activeSider = siders[0].children[0]
            } else {
              this.sidebarShow = false
              this.activeSider = siders[0]
            }
          } else {
            this.sidebarShow = true
            if (siders[0].children?.length > 0) {
              this.activeSider = siders[0].children[0]
            } else {
              this.activeSider = siders[0]
            }
          }
        }
      }
      // console.log(defaultRoute)
      router.push(defaultRoute)
    },
    updateActiveSider(indexPath: string[]) {
      if (indexPath.length === 1) {
        // 一级
        const target = this.siders.find((item: any) => item.path === indexPath[0])
        if (target) {
          this.activeSider = target
        } else {
          this.activeSider = {}
        }
      } else if (indexPath.length === 2) {
        // 二级
        const level1 = this.siders.find((item: any) => item.path === indexPath[0])
        if (level1) {
          if (level1.children?.length) {
            const level2 = level1.children.find((item: any) => item.path === indexPath[1])
            if (level2) {
              this.activeSider = level2
            } else {
              this.activeSider = null
            }
          } else {
            this.activeSider = level1
          }
        } else {
          this.activeSider = null
        }
      } else {
        // 其他
        this.activeSider = null
      }
    }
  }
})
